{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e6cc5391",
   "metadata": {},
   "source": [
    "***  \n",
    "* 2022/09/13  \n",
    "* 课堂笔记  \n",
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "07100516",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 5)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 上周回顾 （range()）:range:范围\n",
    "range(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c4f9ac6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# for+range ：for可以执行循环 ， range决定了循环体的执行次数\n",
    "\n",
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "30dcaff5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "4\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10,3):  #stpe = 3\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1e230a5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(2,5)) # 产生了 5-2个值  ，start = 2  stop = 5  每一次增长 1 个单位 (step)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a8b752a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/xxyw/index0.htm\n",
      "该次停留 2 秒\n",
      "https://www.nfu.edu.cn/xxyw/index1.htm\n",
      "该次停留 5 秒\n",
      "https://www.nfu.edu.cn/xxyw/index2.htm\n",
      "该次停留 1 秒\n",
      "https://www.nfu.edu.cn/xxyw/index3.htm\n",
      "该次停留 2 秒\n",
      "https://www.nfu.edu.cn/xxyw/index4.htm\n",
      "该次停留 5 秒\n"
     ]
    }
   ],
   "source": [
    "### 实战练习: 快速生成 某个网站的所有页码的URL队列\n",
    "\n",
    "import random\n",
    "import time\n",
    "南方学院学校要闻 = \"https://www.nfu.edu.cn/xxyw/index\"\n",
    "页面后缀 = \".htm\"\n",
    "page = 5\n",
    "for i in range(page):\n",
    "    print(南方学院学校要闻 + str(i) + 页面后缀)  # 字符串拼接\n",
    "    random_num = random.randint(1,5) # 设定变量，随机停留多少秒\n",
    "    print(\"该次停留\",random_num,'秒') # 随机停留多少秒\n",
    "    time.sleep(random_num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98c00d54",
   "metadata": {},
   "source": [
    "# 本周内容  \n",
    "  \n",
    "* type():检查类型  \n",
    "\n",
    "> 1.数值  \n",
    ">> 1.int  \n",
    ">> 2.float  \n",
    ">> 3.complex  \n",
    "\n",
    "> 2.字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6973aade",
   "metadata": {},
   "source": [
    "### 1.Number数值  \n",
    " > 1.种类  \n",
    " >> int(整数)  \n",
    " >> float(浮点数)  \n",
    " >> complex(复数)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5a58a981",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "## int\n",
    "print(6,type(6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d33f3b5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.1 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "## float\n",
    "print(1.1,type(1.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f940c14f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5+3j) <class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "## complex(虚数，复数)\n",
    "c = 5 + 3j\n",
    "print(c,type(c))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fb90cef",
   "metadata": {},
   "source": [
    "### 2.字符串（string）  \n",
    "> 1.基本内容  \n",
    "> 2.关于字符串的方法及函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "76523d8c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alex <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# 字符串要放在引号里，单、双、三皆可\n",
    "name = \"alex\"   ## 双引号\n",
    "print(name,type(name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "717cd1e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alex <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "name = 'alex'   ## 单引号\n",
    "print(name,type(name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "688cc51c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alex <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "name = '''alex'''   ## 三引号\n",
    "print(name,type(name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "797084b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"don't\""
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 斜号后的符号不表示\n",
    "'don\\'t'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5fdd240b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ale  x <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# 三引号可保存一切（包括空行、空格和其他语言格式）\n",
    "# 可存其他语言，格式不变\n",
    "\n",
    "name = '''ale  x'''\n",
    "print(name,type(name))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24c7974a",
   "metadata": {},
   "source": [
    "### 2.2 关于字符串的方法及函数  \n",
    "> 1.字符串的定义  \n",
    ">> 1.Single quotes: 'allows embedded \"double\" quotes'  \n",
    ">> 2.Double quotes: \"allows embedded 'single' quotes\"  \n",
    ">> 3.Triple quoted: '''Three single quotes''', \"\"\"Three double quotes\"\"\"  \n",
    "  \n",
    "  \n",
    "> 2.in 检查成员对象  \n",
    "> 3.字符串的拼接  \n",
    "> 4.字符串的方法  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c47af147",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广州南方学院\n"
     ]
    }
   ],
   "source": [
    "# 关于有序性,字符串具有有序性\n",
    "\n",
    "title = \"广州南方学院\"\n",
    "print(title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d1bf4958",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[0] # 取单一值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "bb61bd07",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'院'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[-1] # 取单一值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "840b35f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取一定范围的值，此方法称为“切片”\n",
    "\n",
    "# 从0取到2，但不包括2\n",
    "\n",
    "title[0:2] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "9e10c98f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[:2] # 取一定范围的值，此方法称为“切片”；“0”可省"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c6f40939",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(title) # 检查文本的总长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "5840f3e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广\n",
      "州\n",
      "南\n",
      "方\n",
      "学\n",
      "院\n"
     ]
    }
   ],
   "source": [
    "for i in title:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19200f7b",
   "metadata": {},
   "source": [
    "#### enumerate 枚举，既可以获取内容，又可以获取到 索引值 index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "5ca80c77",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 广\n",
      "1 州\n",
      "2 南\n",
      "3 方\n",
      "4 学\n",
      "5 院\n"
     ]
    }
   ],
   "source": [
    "# enumerate 枚举，既可以获取内容，又可以获取到 索引值 index\n",
    "\n",
    "for i,j in enumerate(title):\n",
    "    print(i,j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "db44b749",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, '广')\n",
      "(1, '州')\n",
      "(2, '南')\n",
      "(3, '方')\n",
      "(4, '学')\n",
      "(5, '院')\n"
     ]
    }
   ],
   "source": [
    "for i in enumerate(title):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "c19fe8b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 书本P59 in--检查成员关系\n",
    "\"广州\" in title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3788ca88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 广\n",
      "广州这个词在这段文本中\n",
      "1 州\n",
      "2 南\n",
      "3 方\n",
      "4 学\n",
      "5 院\n"
     ]
    }
   ],
   "source": [
    "for i,j in enumerate(title):\n",
    "    print(i,j)\n",
    "    if j == \"广\":\n",
    "        if title[i+1] == \"州\":\n",
    "            print(\"广州这个词在这段文本中\")  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "8a0339be",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在的\n"
     ]
    }
   ],
   "source": [
    "# 如何判断一段文字中是否存在“广州”\n",
    "for i,j in enumerate(title):\n",
    "    if j == \"广\":\n",
    "        if title[i+1] == \"州\":\n",
    "            print(\"在的\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "def10431",
   "metadata": {},
   "source": [
    "* 字符串的拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "bb225e6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hi,cola\n"
     ]
    }
   ],
   "source": [
    "print(\"hi\"+\",\"+\"cola\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "ac4a92fb",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [58]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m#字符串与数字不可连接\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhi\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;241m+\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcola\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "#字符串与数字不可连接\n",
    "\n",
    "print(\"hi\"+ 1 +\"cola\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "78e2b26a",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can't multiply sequence by non-int of type 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [60]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 字符串不可用来运算\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m2\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m3\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\n",
      "\u001b[1;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'str'"
     ]
    }
   ],
   "source": [
    "# 字符串不可用来运算\n",
    "\n",
    "\"2\" * \"3\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "a3bccd56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'222'"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串与数值的错误案例\n",
    "a = \"2\"\n",
    "b = 3\n",
    "\n",
    "a * b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "e716632b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 正确案例\n",
    "a = 2\n",
    "b = 3\n",
    "\n",
    "a * b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65bfeead",
   "metadata": {},
   "source": [
    "* 字符串的方法  \n",
    "> 1.str.capitalize() 首字母大写  \n",
    "> 2.str.count() 数量  \n",
    "> 3.str.split() 拆分字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "290a409c",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"cola\" "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "26e9b553",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Cola'"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.capitalize() #首字母大写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "97e94ac0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.count(\"o\") #检查某个字符的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "df3a5a93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['c', 'la']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.split(\"o\") #从某个字符断开拆分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "043b6b3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['cola']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 可以搜索停止词字典，进行文本拆分，然后进一步做分析\n",
    "name.split(\"。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b66faa0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
